package cn.epsilon3.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import cn.epsilon3.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description mybatis-plus元对象配置
 * @author: epsilon3
 * @date: 2023/8/23 10:22
 */
@Component
public class MetaObjectConfig implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"createUser", String.class,SecurityUtils.currentAccount().getPrincipal().getId());
        this.strictInsertFill(metaObject,"updateUser", String.class,SecurityUtils.currentAccount().getPrincipal().getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateUser", String.class,SecurityUtils.currentAccount().getPrincipal().getId());
    }
}
